//
//  HomeVC.m
//  saler-ios
//
//  Created by 王亚陆 on 17/5/27.
//  Copyright © 2017年 sayimba. All rights reserved.
//

#import "HomeVC.h"
#import "TransportVC.h"
#import "RatesVC.h"
//#import "BusinessVC.h"
#import "SDCycleScrollView.h"
#import "FreightVC.h"
#import "LoginViewController.h"
#define TwoBtnWeith Main_Width/2
#define SixBtnWeith Main_Width/6
#define ThreeBtnWeith Main_Width/3
#define FourBtnHeith 111/2
#define  SDCycleScrollViewH 164.0f

@interface HomeVC ()<SDCycleScrollViewDelegate>
{
    UIView * nav;
    Common * com;
    RootViewController * TabBar;
    
    //分页控件的对象指针
    NSMutableDictionary *dataDic;
    NSMutableDictionary *userDic;
    
    ///轮播图数据
    NSMutableArray *scrollArr;
    
    NSTimer * freshTimer;
    SDCycleScrollView *cycleScrollView;
}

@property (nonatomic,strong) UIView * backview1;
@property (nonatomic,strong) UIView * backview2;

@property (nonatomic,strong) UILabel * UserNameLabel;
@property (nonatomic,strong) UILabel * UserMoneyLabel;
@property (nonatomic,strong) UILabel * UpTimeLabel;



@end

@implementation HomeVC

- (void)viewDidLoad {
    [super viewDidLoad];

    nav = [self createNavView];
    [self createNavTitle:@"慧运车"];
    
    self.view.backgroundColor = UIviewBackColor;
    [self.view addSubview:nav];
    
    [self createScrollImage];
    [self createView2];
    [self requsetTopScrollViewData];
    
}



#pragma mark - 轮播图数据
- (void)requsetTopScrollViewData {
    
    NSString * string = [NSString stringWithFormat:@"%@?clientType=30&appType=20",banner_list_Url];
    __weak typeof(self) weakSelf = self;
    
    [Common requestWithUrlString:string contentType:@"application/json" finished:^(id responseObj) {
        
        int success = [responseObj[@"success"] intValue];
      
        NSLog(@"%@",responseObj[@"message"]);
        if (success == 1)
        {
            scrollArr = responseObj[@"data"];
            
            ///创建轮播视图
            [weakSelf createScrollImage];
        }
        
    } failed:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];
}


- (void)createScrollImage {
    
    NSMutableArray *arr = @[].mutableCopy;
    
    NSMutableArray *urlArr = @[].mutableCopy;
    
    for (int i = 0; i < scrollArr.count; i++)
    {
        [arr addObject:scrollArr[i][@"picUrl"]];
        
        [urlArr addObject:scrollArr[i][@"picHref"]];
    }
    
    CGRect frame = CGRectMake(0, NaviStatusH, screenWidth, SDCycleScrollViewH * kHeight);
    
    
    cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:frame
                                                                            delegate:self
                                                                    placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    cycleScrollView.userInteractionEnabled = NO;
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    
    //自定义分页控件小圆标颜色
    cycleScrollView.currentPageDotColor = [UIColor whiteColor];
    
    [self.view addSubview:cycleScrollView];
    
    cycleScrollView.urlArr = urlArr;
    
    //--- 模拟加载延迟
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScrollView.imageURLStringsGroup = arr;
        
    });
    
}


-(void)createView2{
    
    _backview2 = [[UIView alloc]init];
    [self.view addSubview:_backview2];
    [_backview2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(cycleScrollView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(Main_Width, Main_Width/2));
    }];
    NSArray * arrName = @[@"物流运输",
                          @"运价查询"].mutableCopy;
    for (int i=0; i<arrName.count; i++) {
        
        
        UIView * view = [[UIImageView alloc]init];
        view.backgroundColor = WhiteColor;
        [_backview2 addSubview:view];
        
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_backview2.mas_left).with.offset(180*kWidth*i+10*kWidth);
            make.top.equalTo(cycleScrollView.mas_bottom).with.offset(15*kHeight);
            make.size.mas_equalTo(CGSizeMake(175*kWidth, 115*kHeight));
        }];

        UIImageView * image = [[UIImageView alloc]init];
        NSString * imageName = [NSString stringWithFormat:@"home_image%d",i];
        image.image = [UIImage imageNamed:imageName];
        image.tag = 100+i;
        [view addSubview:image];
        
        UILabel * label = [UILabel createUIlabel:arrName[i] andFont:FontOfSize14 andColor:BlackColor];
        [_backview2 addSubview:label];
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i+1;
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_backview2 addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(image);
            make.size.mas_equalTo(CGSizeMake(175*kWidth,115*kHeight));
        }];
        
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(view.mas_centerX);
            make.centerY.equalTo(view.mas_centerY).with.offset(-10*kHeight);
            make.size.mas_equalTo(CGSizeMake(47*kWidth, 47*kHeight));
        }];

        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(image.mas_centerX);
            make.top.mas_equalTo(image.mas_bottom).with.offset(10*kHeight);
        }];

    }
}


-(void)buttonClickImage:(UIButton*)sender{
    
    NSInteger tag = sender.tag;
    UIImageView * image = (UIImageView*)[self.view viewWithTag:tag+99];
    image.image = [UIImage imageNamed:[NSString stringWithFormat:@"shouye%ld",(long)tag]];
}



-(void)buttonClick:(UIButton*)sender{
    
    
    switch (sender.tag) {
        case 1:{
            TransportVC * sport = [[TransportVC alloc]init];
            [self.navigationController pushViewController:sport animated:YES];
        }
            break;
            
        case 2:{
            FreightVC * rates = [[FreightVC alloc]init];
            [self.navigationController pushViewController:rates animated:YES];
            
        }
            break;

        default:
            break;
    }
}


-(void)viewWillAppear:(BOOL)animated{
    
    RootViewController *rootVC = [RootViewController defaultsTabBar];
    [rootVC setTabBarHidden:NO];

    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString *string = [userDefaults objectForKey:@"login"];
    
    
    if (string == nil ) {
        
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        UINavigationController * loginNC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        [self presentViewController:loginNC animated:YES completion:nil];
    }
    
    
    
    NSString * strVersion= [NSString stringWithFormat:@"%@",versionURL];
    [Common requestWithUrlString:strVersion contentType:@"application/json" finished:^(id responseObj) {
        
        int success = [responseObj[@"success"] intValue];
        NSLog(@"%@",responseObj[@"message"]);
        if (success == 1) {
            
            NSString * url = responseObj[@"data"][@"url"];
            NSString *version_url = [responseObj[@"data"][@"version"] stringByReplacingOccurrencesOfString:@"." withString:@""];
            
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            NSString * stringVS = [app_Version stringByReplacingOccurrencesOfString:@"." withString:@""];
            
            int old = [stringVS intValue];
            int new = [version_url intValue];
            if (new>old) {
                [self createUIAlertController:responseObj[@"data"][@"releaseNote"] andUrl:url];
            }
        }
        
    } failed:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];

    
    
}

-(void)createUIAlertController:(NSString*)title andUrl:(NSString *)url
{
    UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"版本更新" message:title preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * action =[UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/id1249952647?mt=8"]];
        
    }];
    UIAlertAction * action1 =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/id1249952647?mt=8"]];

    }];
    [alert addAction:action1];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
